#!/usr/local/bin/perl

#set path = ($path /net/eitech/eitech/local/bin)

if (! -d "bad") {
    mkdir(bad, 0777);
}
if (! -d "test") {
    mkdir(test, 0777);
}

open(DONE, "> test/files") || print "Couldn't open test/files\n";
close(DONE);
open(DONE, ">> test/files") || print "Couldn't open test/files\n";

select(DONE);
$| = 1;

push(@files, $ARGV[0]);

foreach $file (@files) {
    # get rid of /../ thingies
    while (index($file, "/../") != -1) {
	$file =~ s/\057[^\057]*\057..\057/\057/g;
    }
    if ($searched{$file} == 0) {
	$filename = $file;
	$filename =~ s/^.*\057//g;
	if (! $filename) {
	    $filename = "UNKNOWN";
	}
	print "$file\n";
	$searched{$file} = 1;
        if (system("www -w1024 -source $file > test/$filename")) {
	    print "www failed on $file\n";
	    unlink("test/$filename");
	    next;
	}
	open(FILE, "gethtmls test/$filename $file |");
	if (-e "core") {
	    unlink("core");
	    rename("test/$filename", "bad/$filename");
	    print "gethtmls failed on $file\n";
	    close(FILE);
	}
	else {
	    @tmp = <FILE>;
	    chop(@tmp);
	    splice(@files, $#files+1, 0, grep(/.html$/, @tmp));
	    close(FILE);
	    unlink("test/$filename");
	}

    }
}
print "Files:\n";
foreach $key (sort keys(%searched)) {
    print "$key\n";
}
#        gethtmls test/$filename $file /tmp/ghs.$$ > /tmp/errs.$$
#        if ($status == 0) then
#	    if ($#files < 2000) then
#		set new = `grep '^http:[^ ]*html$' /tmp/ghs.$$`
#	    else
#		set new = ()
#	    endif
#	    cat /tmp/errs.$$ >> test/files
#	    echo $file >> test/files
#	    rm -f test/$filename /tmp/ghs.$$ /tmp/errs.$$
#        else
#	    echo $file >> test/files
#            mv test/$filename bad
#	    rm -f /tmp/ghs.$$ /tmp/errs.$$
#            set new = ()
#        endif
#    else
#	#echo $file >> test/files
#        set new = ()
#        echo ignoring $file
#    endif
#    set files = ($files[2-$#files] $new)
#end
